package jupp;

import java.awt.image.BufferedImage;

import java.io.*;

// import java.nio.file.*; // Java 7
import java.util.Scanner;

import javax.imageio.ImageIO;

import jupp.AddressJupp.Address;

public class AddressPhotoJupp
{
	public static class AddressPhoto extends Address
	{
		public final static long serialVersionUID = 42l;
		protected byte[] image;
		public AddressPhoto(String n, String w, String e, String p, byte[] i)
		{
			super(n, w, e, p);
			image = i;
		}
		public String toString()
		{
			StringBuilder sb = new StringBuilder();
			sb.append(name);
			if (web != null)
				sb.append(" ").append(web);
			if (email != null)
				sb.append(" ").append(email);
			if (phone != null)
				sb.append(" phone:").append(phone);
			if (image != null)
				sb.append(" (photo)");
			return sb.toString();
		}
		public void writePhoto()
		{
			if (image != null)
			{
				try
				{
					File f = new File(name + ".jpeg");
					BufferedImage bi = ImageIO.read(new ByteArrayInputStream(image));
					ImageIO.write(bi, "jpg", f);
					System.out.println("Wrote an image file for " + name + " to " + f.getAbsolutePath());
				}
				catch (Exception e)
				{ e.printStackTrace(); }
			}
		}
	}
	public static void main(String[] args) throws Exception
	{
		JuppMap<Address> addresses = new JuppMap<Address>("address.jupp");

		Scanner scanner = new Scanner(System.in);
		while (true)
		{
			System.out.println("(F)ind, (L)ist, (N)ew, (D)elete or (Q)uit");
			String choice = scanner.nextLine();
			switch (choice.toUpperCase().charAt(0))
			{
				case 'F':
					System.out.println("Name to find?");
					String name = scanner.nextLine().toLowerCase();
					for (String key : addresses.getKeys())
						if (key.toLowerCase().contains(name))
						{
							Address address = addresses.get(key);
							System.out.println("Found: " + address);
							if (address instanceof AddressPhoto)
								((AddressPhoto)address).writePhoto();
						}
					System.out.println("Find complete");
					break;
				case 'L':
					System.out.println("List: " + addresses.getKeys());
					break;
				case 'N':
					System.out.println("Name?");
					name = scanner.nextLine();
					System.out.println("Homepage?");
					String w = scanner.nextLine();
					System.out.println("Email?");
					String e = scanner.nextLine();
					System.out.println("Phone?");
					String p = scanner.nextLine();
					System.out.println("Photo file?");
					String i = scanner.nextLine();
					/* JDK 7
					byte[] image = null;
					if (i.length() > 0)
						image = Files.readAllBytes(new File(i).toPath());
					/ * end JDK 7 */
					/* JDK 6 */
					byte[] image;
					File photoFile = new File(i);
					int photoSize = (int)photoFile.length(); // can't handle files bigger than GB
					if (photoSize == 0)
						image = null;
					else
					{
						image = new byte[photoSize];
						int remaining = photoSize, off = 0;
						FileInputStream is = new FileInputStream(photoFile);
						while (remaining > 0)
						{
							int len = is.read(image, off, remaining);
							remaining -= len;
							off += len;
						}
					}
					/* end JDK 6 */
					addresses.put(name, new AddressPhoto(name
					 , w.length() > 0?w:null
					 , e.length() > 0?e:null
					 , p.length() > 0?p:null
					 , image));
					break;
				case 'D':
					System.out.println("Name to remove?");
					name = scanner.nextLine();
					addresses.remove(name);
					System.out.println("Removed: " + name);
					break;
				case 'Q':
					System.out.println("Bye!");
					System.exit(0);
					break;
				default:
					System.out.println("Something wrong with '" + choice + "'");
			} // switch user choice
		}
	}
}
